import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () => import ('../components/Login/login')
const Home = () => import ('../components/Home/home')
const Welcome = () =>import ('../components/Welcome/welcome')
const Users = () =>import ('../components/children/Users/users')
const Rights = () =>import ('../components/children/power/rights')
const Roles = () =>import ('../components/children//power/roles')
const Categories = ()=>import ('../components/children/goods/categories')
const Params = () =>import ('../components/children/goods/params')
const Goods = () =>import ('../components/children/goods/goods')
const Add = () =>import ('../components/children/goods/add')
const Order = () =>import ('../components/children/order/order')
const Report = () =>import ('../components/children/report/report')
Vue.use(VueRouter)

  const routes = [
  {path:'/',redirect:'/login'},
  {path:'/login',component:Login},
  {
    path:'/home',
    component:Home,
    redirect:'/welcome',
    children:[
      {path:'/welcome',component:Welcome},
      {path:'/users',component:Users},
      {path:'/rights',component:Rights},
      {path:'/roles',component:Roles},
      {path:'/categories',component:Categories},
      {path:'/params',component:Params},
      {path:'/goods',component:Goods},
      {path:'/goods/add',component:Add},
      {path:'/orders',component:Order},
      {path:'/reports',component:Report}
    ]
  }
]

const router = new VueRouter({
  routes,
  mode:'history'
})

//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
   //to要访问的路径
   //from代表从哪个路径跳转过来
   //next可以下一步了
   if(to.path=='/login'){
     return next ()
   }else{
     const tokenStr = window.sessionStorage.getItem('token')
      if(tokenStr){
       return next()
      }else{
       return next('/login')
      }
   }
})

export default router
